//
//  recv_client_request.cpp
// ~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2013-2014 Razvan Coca (razvan dot coca at  gmail dot com)
// This is mostly generated code. Generator is distributed under GPL. Edit it to suit your needs.
// 



#include "recv_client_request.hpp"
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/asio.hpp>

#include "patterns/command.hpp"
#include "patterns/handlers.hpp"

#include "logging/logging.hpp"

namespace pcmd=patterns::command;
namespace phdl=patterns::handlers;
namespace ba=boost::asio;
namespace bs=boost::system;
namespace bt=boost::posix_time;

#include "components/defs/util_types.hpp"
#include "components/defs/server_processor.hpp"
#include "components/protocol/recv_client_request.hpp"
namespace components{
namespace protocol{


bool recv_client_request::cancel()
{

    try{
        bool retcode=true;
        LOG(WARNING)<<name_<<"::"<<__FUNCTION__;
        retcode &= (recv_service_request_ && recv_service_request_->cancel());
        retcode &= (decode_enc_pb_request_ && decode_enc_pb_request_->cancel());
        return retcode;
    }
    catch(std::exception &e){
        LOG(WARNING)<<name_<<"::"<<__FUNCTION__<<":"<<e.what();
        return false;
    }


}

boost::shared_ptr<recv_client_request> recv_client_request::create(ba::io_service& ios,ba::ip::udp::socket& msocket,crypto_buf_t& iv,crypto_buf_t& key,stats_message_server_processor& message_processor)
{

    return boost::shared_ptr<recv_client_request>(new recv_client_request(ios,msocket,iv,key,message_processor));
}

void recv_client_request::handle_decode_enc_pb_request(const crypto_error& eec)
{


    LOG(INFO)<<name_<<"::"<<__FUNCTION__<<"["<<eec.GetWhat()<<"]";

    if(!phdl::is_error(eec))
    {
        stats_message_t decrypted_message;
        if(!decrypted_message.ParseFromString(decrypt_buf_))
        {
            LOG(ERROR)<<__FUNCTION__<<"error parsing from decrypted message";
            return;
        }
        bs::error_code ec;
        if(!message_processor_(decrypted_message,peer_,ec))
        {
            LOG(ERROR)<<__FUNCTION__<<"error processing decrypted message";
            return;
        }

    }

    //recv_service_request_ && recv_service_request_->cancel();


}

void recv_client_request::handle_recv_service_request(const bs::error_code& ec,std::size_t bytes_transferred)
{


    LOG(INFO)<<name_<<"::"<<__FUNCTION__<<"["<<ec<<"]";
    if(!ec)
    {
        recv_buf_.resize(bytes_transferred);
    }

    //decode_enc_pb_request_ && decode_enc_pb_request_->cancel();


}

void recv_client_request::init_components()
{

    if(!recv_service_request_) recv_service_request_ = components::network::recv_service_request::create(ios_,msocket_);
    if(!decode_enc_pb_request_) decode_enc_pb_request_ = components::protocol::decode_enc_pb_request::create(ios_,iv_,key_,message_processor_);
}

recv_client_request::recv_client_request(ba::io_service& ios,ba::ip::udp::socket& msocket,crypto_buf_t& iv,crypto_buf_t& key,stats_message_server_processor& message_processor)
    :ios_(ios), msocket_(msocket), iv_(iv), key_(key), message_processor_(message_processor)
{

    name_=std::string(__FUNCTION__);
    DLOG(INFO)<<name_<<"()";
    init_components();
}

recv_client_request::~recv_client_request()
{
    DLOG(INFO)<<name_<<"::"<<__FUNCTION__<<std::endl;
}
}//end namespace protocol
}//end namespace components

